<%@ page language="java" import="java.util.*" pageEncoding="utf-8" %>
<%@include file="/commons/taglibs.jsp" %>
<!DOCTYPE html>
<html>
<fmt:bundle basename="res">
<head>
    <meta charset="utf-8">
    <title><fmt:message key='ttbl' /></title>
    <jsp:include page="/WEB-INF/commons/commons-header.jsp" flush="true"/>
    <style>
        html {
            background: #fff;
        }
    </style>
</head>

<body>
<div class="layui-container">
    <div class="layui-form" lay-filter="layuiadmin-app-form-list" id="layuiadmin-app-form-list" style="padding: 20px 30px 0 0;">

        <form:form commandName="goods" method="${_method }" class="layui-form" id="form">
            <input type="hidden" name="_method" value="${_method }"/>
            <form:hidden path="id"/>

            <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
                <legend><fmt:message key='jbxx' /></legend>
            </fieldset>


            <div class="layui-form-item">
                <label class="layui-form-label"> <b style="color:red">*</b><fmt:message key='spmc' /></label>
                <div class="layui-block">
                    <form:input path="name" readonly="true" class="layui-input" cssStyle="width:200px;display: inline-block; float: left;" lay-verify="required" placeholder="商品名称" style="width: 200px;"/>
                    <a href="javascript:void(0);" id="feijiin-choose-goods" class="layui-btn layui-btn-normal" style='margin-left: 10px;'><fmt:message key='xzsp' /></a>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label"> <b style="color:red">*</b><fmt:message key='spbh' /></label>
                <div class="layui-input-block" style="width:700px;">
                    <form:input path="goodsNo" readonly="true" class="layui-input layui-inline" lay-verify="required" placeholder="<fmt:message key='spbh' />" style="width: 200px;"/>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label"> <b style="color:red">*</b><fmt:message key='spjg' /></label>
                <div class="layui-input-block">
                    <form:input path="defaultPrice" readonly="true" class="layui-input" lay-verify="required" placeholder="<fmt:message key='spbh' />" style="width: 200px;"/>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label"> <b style="color:red">*</b><fmt:message key='scj' /></label>
                <div class="layui-input-block">
                    <form:input path="marketPrice" readonly="true" class="layui-input" lay-verify="required" placeholder="<fmt:message key='spbh' />" style="width: 200px;"/>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label"><b style="color:red">*</b> <fmt:message key='spzkc' /></label>
                <div class="layui-input-block">
                    <form:input path="store" readonly="true" class="layui-input" lay-verify="required" style="width: 200px;"/>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label"><b style="color:red">*</b> <fmt:message key='hdjg' />/label>
                <div class="layui-input-block">
                    <form:input path="bargainPrice" class="layui-input" lay-verify="required" placeholder="<fmt:message key='hdjg' />" style="width: 200px;"/>
                </div>
            </div>


            <div class="layui-form-item">
                <label class="layui-form-label"><b style="color:red">*</b> <fmt:message key='tjyxq' /></label>
                <div class="layui-input-block">
                    <form:input path="beginDateOfValidity" readonly="true" class="layui-input" lay-verify="required" placeholder="<fmt:message key='kssj' />" style="width: 150px;display:inline;"/> -
                    <form:input path="endDateOfValidity" readonly="true" class="layui-input" lay-verify="required" placeholder="<fmt:message key='dqsj' />" style="width: 150px;display:inline;"/>
                </div>
            </div>

            <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
                <legend><fmt:message key='spgg' /></legend>
            </fieldset>

            <div class="layui-form-item attributeArea">
                <label class="layui-form-label"><b style="color:red">*</b> <fmt:message key='spsku' /></label>
                <div class="layui-input-block">
                    <table class="layui-table" lay-size="sm" id="feijin-sku-table">
                        <thead>
                        <tr>
                            <th><fmt:message key='gg' /></th>
                            <th><fmt:message key='sjbh' /></th>
                            <th><fmt:message key='kcs' /></th>
                            <th><fmt:message key='xsj' /></th>
                            <th><fmt:message key='tjhdkc' /> <b style="color:red">*</b></th>
                            <th><fmt:message key='tjhdjg' /> <b style="color:red">*</b></th>
                        </tr>
                        </thead>
                        <tbody>
                        <c:forEach items="${goods.skus}" var="sku">
                            <tr>
                                <td>${sku.skuNames}</td>
                                <td>${sku.code}</td>
                                <td class="feijin-old-store">${sku.store}</td>
                                <td class="feijin-old-price">${sku.price}</td>
                                <td><input type="text" class="layui-input feijin-store" data-id="${sku.id}" value="${sku.bargainStore}"/></td>
                                <td><input type="text" class="layui-input feijin-price" data-id="${sku.id}" value="${sku.bargainPrice}"/></td>
                            </tr>
                        </c:forEach>
                        </tbody>
                    </table>
                </div>
            </div>

            <div class="layui-input-block">
                <input type="hidden" name="skusData" id="skusData"/>
                <div style="display: none"><input type="button" value="<fmt:message key='bc' />" class="layui-btn" id="submit" lay-filter="submit" lay-submit></div>
            </div>

        </form:form>
    </div>
</div>

<div style="display:none;">

</div>


<script>

    $(function () {

        //选择赠品
        $("#feijiin-choose-goods").click(function () {
            layer.open({
                type: 2
                , title: '<fmt:message key='xzcjtjhddsp' />'
                , content: '${web_domain}/merchant/bargain/chooseGoods'
                , maxmin: true
                , area: ['80%', '90%']
                , btn: ['<fmt:message key='qd' />', '<fmt:message key='qx' />']
                , yes: function (index, layero) {

                    var ids = [];

                    layero.find('iframe').contents().find(".feijin-check:radio:checked").each(function (index, item) {
                        var id = $(item).val();
                        ids.push(id);
                    });

                    layer.closeAll();
                    location.href = "${web_domain}/merchant/bargain/create?id=" + ids.join("");


                }
            });
        });

        layui.use(['form', 'laydate'], function () {

            var form = layui.form;
            var laydate = layui.laydate;

            laydate.render({
                elem: '#beginDateOfValidity',
                format: 'yyyy-MM-dd HH:mm',
                type: 'datetime'
            });

            laydate.render({
                elem: '#endDateOfValidity',
                format: 'yyyy-MM-dd HH:mm',
                type: 'datetime'
            });

            form.on('submit(submit)', function (data) {

                var data = [];

                var content = '';

                $(".feijin-store").each(function (index, item) {
                    var store = $(item).val();
                    var id = $(this).attr("data-id");
                    var price = $(".feijin-price:eq(" + index + ")").val();

                    var oldStore = $(".feijin-old-store:eq(" + index + ")").html();
                    var oldPrice = $(".feijin-old-price:eq(" + index + ")").html();

                    if (parseFloat(price) > parseFloat(oldPrice)) {
                        content = "<fmt:message key='hdjbyxdyyxsj' />";
                        return false;
                    }


                    if (parseInt(store) > parseInt(oldStore)) {
                        content = "<fmt:message key='hdkcbyxdyykc' />";
                        return false;
                    }

                    data.push(id + "-" + store + ":" + price);
                });

                if (content != '') {
                    layer.msg(content);
                    content = '';
                    return false;
                }

                $("#skusData").attr("value", data.join(";"));


                $("#form").ajaxSubmit({
                    success: function (result) {
                        if (result.status) {
                            var index = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(index);
                            parent.$("#search").click();
                        } else {
                            layer.alert(result.message);
                        }
                    }
                });
            });

        });

    });
</script>
</body>
</fmt:bundle>
</html>